package map.utils;

import android.util.Log;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import map.MapBaseManager;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/26 14:04
 */
public final class MapManagerFactory {

    private static final String TAG = "ManagerFactory";


    private static final Map<Class<? extends MapBaseManager>, MapBaseManager> content = new ConcurrentHashMap<>();


    @SuppressWarnings("unchecked")
    public static <T extends MapBaseManager> T get(Class<T> clazz) {
        MapBaseManager mapBaseManager = content.get(clazz);
        if (mapBaseManager == null) {
            try {
                mapBaseManager = clazz.newInstance();
                mapBaseManager.onCreate();
                content.put(clazz, mapBaseManager);
            } catch (Exception e) {
                Log.e(TAG, "instance: " + e.getMessage());
            }
        }

        return (T) mapBaseManager;
    }


    public static void onDestroy() {
        for (MapBaseManager value : content.values()) {
            value.onDestroy();
        }
        content.clear();
    }
}
